package BZimmerman.AndroidWars;

import java.util.ArrayList;

import android.graphics.PointF;
import android.util.Log;

public class heroInfo {
	private PointF location, lastLocation;
	private PointF cursor, lastCursor;
	private boolean screenMoved;
	private PointF ratio;
	private ArrayList<PointF> foundPath;
	private ArrayList<Integer> pOrientation;
	heroInfo(PointF location){
		System.out.println(location.x + " : " + location.y);
		this.location = new PointF(location.x,location.y);
		this.cursor = new PointF(location.x,location.y);
		this.lastCursor = new PointF(location.x,location.y);
		this.lastLocation = new PointF(location.x,location.y);
		foundPath = new ArrayList<PointF>();
		pOrientation = new ArrayList<Integer>();
		screenMoved = false;
		ratio = new PointF(0,0);
	}
	public void screenClicked(){
		screenMoved = false;
	}
	public void calcLocation(PointF click, int scale, int[][] map, int size, int movementSquares){
		PointF temp = new PointF((float)Math.floor(click.x/scale), (float)Math.floor(click.y/scale));
		
		if(map[(int) temp.x][(int) temp.y]!=0){
			//This is to make sure that the player isn't moved when the user resizes the screen
			lastCursor.x = cursor.x;
			lastCursor.y = cursor.y;
			lastLocation.x = location.x;
			lastLocation.y = location.y;
		
			if (temp.equals(cursor.x,cursor.y)){
				location.x = temp.x;
				location.y = temp.y;
			}
			//printPath();
			
			cursor.x = temp.x;
			cursor.y = temp.y;
			
			foundPath.clear();
			pOrientation.clear();
			shortestPath path = new shortestPath(map,location, cursor,size, movementSquares);
			foundPath = new ArrayList<PointF>(path.returnPath());
			setOrientation();
		}
	}
	public void revertToLastMoved(){
		screenMoved = true;
		
		//cursor.x = lastCursor.x;
		//cursor.y = lastCursor.y;
		
		//location.x = lastLocation.x;
		//location.y = lastLocation.y;
	}
	public PointF getCursor(){
		return cursor;
	}
	public PointF getLocation(){
		return location;
	}
	public boolean screenMoved(){
		return screenMoved;
	}
	
	public void setOrientation(){
		//1:up 2:down 3:left 4:right
		for(int i = 0; i < foundPath.size()-1; i++){
			//left
			if(foundPath.get(i).x > foundPath.get(i+1).x 
					&& foundPath.get(i).y == foundPath.get(i+1).y){pOrientation.add(3);}
			//right
			else if(foundPath.get(i).x < foundPath.get(i+1).x 
					&& foundPath.get(i).y == foundPath.get(i+1).y){pOrientation.add(4);}
			//up
			else if(foundPath.get(i).x == foundPath.get(i+1).x 
					&& foundPath.get(i).y > foundPath.get(i+1).y){pOrientation.add(1);}
			//down
			else if(foundPath.get(i).x == foundPath.get(i+1).x 
					&& foundPath.get(i).y < foundPath.get(i+1).y){pOrientation.add(2);}
			else{pOrientation.add(0);}
		}
		pOrientation.add(0);
	}
	public ArrayList<Integer> fOrientation(){
		return (ArrayList<Integer>) pOrientation.clone();
	}
	public int returnOrientationSize(){
		return pOrientation.size();
	}
	public ArrayList<PointF> fPath(){
		return (ArrayList<PointF>) foundPath.clone();
	}
	public void printPath(){
		for(PointF point: foundPath){
			System.out.println("Point: " + point.x + ":" + point.y);
		}
	}
	public void setRatio(float height, float width){
		ratio.y = height/width;
		ratio.x = width/height;
		System.out.println("Ratio.x: "+ratio.x +" Ratio.y: " + ratio.y);
	}
	public boolean cursorOnHero(){
		return (location.equals(cursor.x,cursor.y));
	}
	//This function is used to reevaluate the margins when the character is moved
	public boolean outOfBox(PointF margins, int scale, float width, float height){
		if(		  (-location.x*scale >= margins.x-(width*.10))
				||(-location.y*scale >= margins.y-(height*.10))
				||(-location.x*scale <= margins.x-(width*.90))
				||(-location.y*scale <= margins.y-(height*ratio.y))
			)
				{
			return true;
		}
		return false;
	}
	
	public PointF getRatio(){
		return ratio;
	}
}
